package com.aqie.rabbit.api.exchange.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProducerTest {
    private static final String IP_ADDRESS = "127.0.0.1";
    private static final String VIRTUAL_HOST = "/";
    private static final String EXCHANGE_NAME = "exchange_topic";
    private static final String ROUTING_KEY = "topic.news";
    private static final String ROUTING_KEY1 = "topic.weather";
    private static final int PORT = 5672;

    public static void main(String[] args) throws IOException, TimeoutException {
        //1 创建一个ConnectionFactory, 并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(IP_ADDRESS);
        connectionFactory.setPort(PORT);
        connectionFactory.setVirtualHost(VIRTUAL_HOST);

        //2 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        //3 通过connection创建一个Channel
        Channel channel = connection.createChannel();

        //4 通过Channel发送数据
        String msg = "Hello RabbitMQ! Topic";
        //1 exchange   2 routingKey
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY1, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());

        //5 记得要关闭相关的连接
        channel.close();
        connection.close();
    }
}
